<template>
	<view>
		<view class="u-text-center m-bg-main">
			<view class="m-logout-button" v-if="user.id" @click="doLogout()">退 出</view>
			<view class="u-p-30"><u-avatar :src="user.headimg" mode="circle" size="230"></u-avatar></view>
			<view class="u-p-b-30">
				<view class="u-font-lg u-content-color u-p-5">{{ user.nickname }}</view>
				<view class="u-font-xs u-tips-color u-p-5">
					<view v-if="user.id">
						<text>[ VIP{{ user.vip_code || '' }} ] {{ user.vip_name || '' }}</text>
					</view>
				</view>
			</view>
		</view>

		<view class="u-m-t-20">
			<u-cell-group><u-cell-item icon="rmb-circle" title="支付"></u-cell-item></u-cell-group>
		</view>

		<view class="u-m-t-20">
			<u-cell-group>
				<u-cell-item v-for="item in cells" :key="item.name" :icon="item.icon" @click="$u.route(item.path, item.params)" :title="item.name"></u-cell-item>
			</u-cell-group>
		</view>

		<view class="u-m-t-20">
			<u-cell-group><u-cell-item icon="setting" title="设置"></u-cell-item></u-cell-group>
		</view>

		<view class="u-p-b-20"></view>

		<m-login ref="login"></m-login>
	</view>
</template>

<script>
export default {
	data() {
		return {
			init: 0,
			user: {},
			cells: [
				{
					name: '我的收藏',
					icon: 'star',
					path: '',
					params: {}
				},
				{
					name: '我的相册',
					icon: 'photo',
					path: '',
					params: {}
				},
				{
					name: '我的卡券',
					icon: 'coupon',
					path: '',
					params: {}
				},
				{
					name: '我的订单',
					icon: 'list-dot',
					path: 'pages/order/index',
					params: {}
				},
				{
					name: '收货地址',
					icon: 'map',
					path: 'pages/address/index',
					params: {}
				}
			]
		};
	},
	mounted() {
		this.init = 1;
		// 用户初始化登录
		this.$refs.login.local().then(user => {
			this.user = user;
		});
	},
	onShow() {
		if (this.init) {
			this.$refs.login.local();
		}
	},
	methods: {
		load() {
			console.log(arguments);
		},
		doLogout() {
			this.$u.login.logout().then(() => {
				this.user = {};
				this.$refs.login.local();
			});
		}
	},
	// 下拉刷新处理
	onPullDownRefresh() {
		// 刷新用户资料
		this.$refs.login.reload().then(ret => {
			this.user = ret;
			setTimeout(() => uni.stopPullDownRefresh(), 1000);
		});
	}
};
</script>

<style lang="scss" scoped>
.m-logout-button {
	top: 20rpx;
	right: 20rpx;
	position: absolute;
}
</style>
